package com.thomas.generator.component;

import com.thomas.generator.vo.Model;
import com.thomas.generator.util.FileUtil;
import com.thomas.generator.util.FreemarkerUtil;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xutao
 * @Package com.thomas.generator.component
 * @Description: ${todo}
 * @date 2019/12/11 15:05
 */
public abstract class AbstractGenerator implements IGenerator {

    public abstract void init()throws Exception;


    @Override
    public void execute() throws Exception {
        FileUtil.runSrcDelete();
        init();
        generator();
        CommonUtil.closeConnection();
        FileUtil.runCopy();
    }

    @Override
    public void generator() throws Exception {
        ResultSet tableSet = getTableSet();
        while (tableSet.next()) {
            String tableName = tableSet.getString("TABLE_NAME");
            boolean checkTableName = CommonUtil.checkTableName(tableName);
            if (!checkTableName) {
                continue;
            }
            ResultSet columnSet = getColumnSet(tableName);
            Map<String, Object> parameter = new HashMap<String, Object>();
            String className = CommonUtil.getClassName(tableName);
            String lowerClassName = className;
            parameter.put("lowerClassName", lowerClassName);
            parameter.put("className", lowerClassName);
            parameter.put("tableName", tableName);
            String primary = null;
            String primaryType = null;
            List<Model> fieldList = new ArrayList<Model>();
            while (columnSet.next()) {
                String columnComment = columnSet.getString("REMARKS");
                String sqlType = columnSet.getString("TYPE_NAME");
                String columnName = columnSet.getString("COLUMN_NAME");
                fieldList.add(new Model(CommonUtil.toFirstLower(CommonUtil.getFileName(columnName)), columnComment, CommonUtil.typeMap.get(sqlType.toUpperCase()),
                        columnComment, CommonUtil.toFirstUp(CommonUtil.getFileName(columnName)), columnName, sqlType, CommonUtil.getJdbcType(sqlType)));
                if (primary == null) {
                    primary = columnName;
                    primaryType = sqlType;
                }
            }
            parameter.put("encode", CommonUtil.EDCODE);
            parameter.put("moduleName", CommonUtil.MODULE_NAME);
            parameter.put("basePackage", CommonUtil.BASE_PACKAGE_PREFIX);
            parameter.put("primaryType", primaryType);
            parameter.put("primary", CommonUtil.toFirstLower(CommonUtil.getFileName(primary)));
            parameter.put("fields", fieldList);
            parameter.put("fieldSize", fieldList.size());
            out(parameter, className);
        }
    }

    public static void out( Map<String,Object> parameter,String className){
        FreemarkerUtil fu = new FreemarkerUtil();
        fu.generator("Model.ftl", parameter,className+".java");
        fu.generator("Dao.ftl", parameter,className+"Mapper.java");
        fu.generator("Mapper.ftl", parameter,className+"Mapper.xml");
        fu.generator("IService.ftl", parameter,className+"Service.java");
        fu.generator("ServiceImpl.ftl", parameter,className+"ServiceImpl.java");
//        fu.generator("JspMgr.ftl", parameter,CommonUtil.toFirstLower(className)+"Mgr.jsp");
        fu.generator("Controller.ftl", parameter,className+"Controller.java");
    }


}
